package dbhelper;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class MyDBConnectionPool {
	private static DataSource dataSource;
	
	static{
		init();
	}
	 public static void init(){
	        try {
	            // init DataSource
	            Context initContext  = new InitialContext();
	            Context envContext  = (Context)initContext.lookup("java:/comp/env");
	            dataSource = (DataSource)envContext.lookup("jdbc/testdb");
	 
	             
	        } catch (NamingException e) {
	            e.printStackTrace();
	        }
	    }    
	    /**
	     * 
	     * @return a connection from the data source
	     */
	    public static Connection getConnection(){
	    	try {
				return dataSource.getConnection();
			} catch (SQLException e) {
				e.printStackTrace();
			}
	    	return null;
	    }
	
}
